﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonFramework.Storage.Cache;
using Back.Xinkui.Common.Const;

namespace Back.Xinkui.Common.Control.Region
{
    public class RegionManager
    {
        private static List<CountryEntity> _CountryList;
        private static List<CountryEntity> CountryList
        {
            get
            {
                if (_CountryList == null)
                {
                    _CountryList = CacheManager.CreateCache(VerifyManageKey.GASGOO_TRADE_REGION).Get<List<CountryEntity>>(VerifyManageKey.GASGOO_TRADE_COUNTRY);
                    if (_CountryList == null)
                    {
                        _CountryList = RegionDataAccess.GetCountry();
                        CacheManager.CreateCache(VerifyManageKey.GASGOO_TRADE_REGION).Set<List<CountryEntity>>(VerifyManageKey.GASGOO_TRADE_COUNTRY, _CountryList);
                    }

                }
                return _CountryList;
            }
        }

        private static List<ProvinceEntity> _ProvinceList;
        public static List<ProvinceEntity> ProvinceList
        {
            get
            {
                if (_ProvinceList == null)
                {
                    _ProvinceList = CacheManager.CreateCache(VerifyManageKey.GASGOO_TRADE_REGION).Get<List<ProvinceEntity>>(VerifyManageKey.GASGOO_TRADE_PROVINCE);
                    if (_ProvinceList == null)
                    {
                        _ProvinceList = RegionDataAccess.GetProvince();
                        CacheManager.CreateCache(VerifyManageKey.GASGOO_TRADE_REGION).Set<List<ProvinceEntity>>(VerifyManageKey.GASGOO_TRADE_PROVINCE, _ProvinceList);
                    }
                    return _ProvinceList;
                }
                return _ProvinceList;
            }
        }

        private static List<CityEntity> _CityList;
        private static List<CityEntity> CityList
        {
            get
            {
                if (_CityList == null)
                {
                    _CityList = CacheManager.CreateCache(VerifyManageKey.GASGOO_TRADE_REGION).Get<List<CityEntity>>(VerifyManageKey.GASGOO_TRADE_CITY);
                    if (_CityList == null)
                    {
                        _CityList = RegionDataAccess.GetCity();
                        CacheManager.CreateCache(VerifyManageKey.GASGOO_TRADE_REGION).Set<List<CityEntity>>(VerifyManageKey.GASGOO_TRADE_CITY, _CityList);
                    }

                }
                return _CityList;
            }
        }

        public static List<CityEntity> GetCity()
        {
            return CityList;
        }

        public static List<CountryEntity> GetCountry()
        {
            return CountryList;
        }

        public static List<CityEntity> GetCityByProvince(int ProvinceID)
        {
            return CityList.Where(u => u.ProvinceID == ProvinceID).ToList();
        }

        public static List<ProvinceEntity> GetProvinceByCountry(int CountryID)
        {
            return ProvinceList.Where(u => u.CountryID == CountryID).ToList();
        }

        public static string GetCityByID(int CityID)
        {
            return CityList.Where(u => u.CityID == CityID).Select(u => u.City).FirstOrDefault();
        }

        /// <summary>
        /// 获取长途区号
        /// </summary>
        /// <param name="CityID"></param>
        /// <returns></returns>
        public static int GetCityCodeByID(int CityID)
        {
            try
            {
                return CityList.Where(u => u.CityID == CityID).Select(u => u.CityCode).FirstOrDefault();
            }
            catch
            {
                return 021;
            }
        }

        public static string GetProvinceByID(int ProvinceID)
        {
            return ProvinceList.Where(u => u.ProvinceID == ProvinceID).Select(u => u.Province).FirstOrDefault();
        }

        public static int GetProvinceIdByName(string provinceName)
        {
            return ProvinceList.Where(u => u.Province == provinceName).Select(u => u.ProvinceID).FirstOrDefault();
        }

        public static int GetCityIdByName(int provinceId, string cityName)
        {
            return CityList.Where(c => c.ProvinceID == provinceId && c.City == cityName).Select(c => c.CityID).FirstOrDefault();
        }
    }
}
